/**
 * Created by Eric on 14-1-5.
 */
define(function (require) {
    var fields = [
        {field: 'userName', align: 'center'},
        {field: 'password', align: 'center'},
        {field: 'tonken', align: 'center'}
    ];
    var md5 = require('lib/plugins/md5');
        return {
            views: ['form','dialog'],
            views_form: {
                target: '#loginPanel',
                widgets: 'form',
                listener: function ($) {
                    $("#userName").bind('change',function(e){
                        $('#errMsg').text("");
                        return false;
                    }),
                    $("#password").bind('change',function(e){
                        $('#errMsg').text("");
                        return false;
                    }),
                    $("#registerBtn").bind('click',function(e){
                        $('#registerMsg').text("")
                        $('#registerDiv').dialog('open');
                        return false;
                    }),
                    $("#submitBtn").bind('click',function(e){
                        if($('#userName').val() == "" )
                        {
                            $('#errMsg').text("请输入用户名！");
                            return false;
                        }
                        if($('#password').val() == "" )
                        {
                            $('#errMsg').text("请输入密码！");
                            return false;
                        }
                        $('#loginForm').ajaxSubmit({
                            url: '/api/bo/login',
                            target: '#loginForm',
                            type: 'POST',
                            dataType: 'json',
                            contentType: 'application/json; charset=utf8',
                            clearForm: true,
                            columns: fields,
                            beforeSubmit: function (formData, jqForm, options) {
                                var pwd = null;
                                var pwdStr = "#tonken|#userName_md5@password";
                                for (i = 0, len = formData.length; i < len; i++) {
                                    var data = formData[i];
                                    if (pwdStr.indexOf("#" + data.name) >= 0) {
                                        pwdStr = pwdStr.replace("#" + data.name, data.value);
                                    }
                                    else if (pwdStr.indexOf("md5@" + data.name) >= 0) {
                                        pwdStr = pwdStr.replace("md5@" + data.name, md5.hex_md5(data.value));
                                    }
                                    if (data.name == 'password') {
                                        pwd = data;
                                    }
                                }
                                pwd['value'] = md5.hex_md5(pwdStr);
                                console.log(pwd['value']);
                                return true;
                            },
                            success: function (data, statusText) {
                                console.log(data);
                                if ("success" == statusText) {
                                    self.location.href = '/page/frame';
                                } else {
                                    console.log(data);
                                }
                                $("#loading").hide();
                            }
                        });
                        return false;
                    })
                }
            },
                views_dialog:{
                    target: '#registerDiv',
                    widgets: 'dialog',
                    options:{
                        title: '注册页面',
                        width: 600,
                        height: 300,
                        closed: true,
                        cache: false,
                        href: '/system/register.html',
                        modal: true
                    },
                    listener: function ($) {
                        $('#registerDiv').dialog({
                            onOpen: function () {
                                $.getJSON('/api/uid', function (data) {
                                    $('#tonken').val(data);
                                });
                            },
                            buttons: [
                                {
                                    text: '保存',
                                    //iconCls: 'icon-save',
                                    handler: function () {
                                        $("#rUserName").bind('change',function(e){
                                            $('#registerMsg').text("");
                                            return false;
                                        }),
                                        $("#rPassword").bind('change',function(e){
                                            $('#registerMsg').text("");
                                            return false;
                                        })
                                        if($('#rUserName').val() == "" )
                                        {
                                            $('#registerMsg').text("请输入用户名！");
                                            return false;
                                        }
                                        if($('#rPassword').val() == "" )
                                        {
                                            $('#registerMsg').text("请输入密码！");
                                            return false;
                                        }
                                        $('#registerForm').ajaxSubmit({
                                            url: '/api/register',
                                            target: '#registerForm',
                                            type: 'POST',
                                            dataType: 'json',
                                            contentType: 'application/json; charset=utf8',
                                            clearForm: true,
                                            columns: fields,
                                            beforeSubmit: function (formData, jqForm, options) {
                                                for (i = 0, len = formData.length; i < len; i++) {
                                                    var data = formData[i];
                                                    if (data.name == 'password') {
                                                        data['value'] = md5.hex_md5(data['value']);
                                                    }
                                                }
                                                return true;
                                            },
                                            success: function (data, statusText) {
                                                if ("success" == statusText) {
                                                    alert("Register successed.");
                                                    $('#registerDiv').dialog('close')
                                                }
                                            }
                                        });
                                    }
                                },
                                {
                                    text: '取消',
                                   // iconCls: 'icon-cancel',
                                    handler: function () {
                                        $('#registerForm').clearForm();
                                        $('#registerDiv').dialog('close');
                                    }
                                }
                            ]
                        })
                    }
                }
            }
});